package logic

import (
	"context"
	"fmt"
	"models"
	"strconv"

	"shop_srv/internal/svc"
	"shop_srv/shop_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type CarListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCarListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CarListLogic {
	return &CarListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CarListLogic) CarList(in *shop_srv.CarListRequest) (*shop_srv.CarListResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("card_%d:*", in.UserId)
	keys, _ := models.RDB.Keys(models.Ctx, key).Result()
	var goods []*shop_srv.Goods
	for _, s := range keys {
		GM, _ := models.RDB.HGetAll(models.Ctx, s).Result()
		num, _ := strconv.Atoi(GM["Number"])
		G := shop_srv.Goods{
			Name:      GM["GoodsName"],
			Number:    int64(num),
			Image:     GM["GoodsTitle"],
			CreatedAt: GM["CreatedAt"],
		}
		goods = append(goods, &G)
	}
	return &shop_srv.CarListResponse{
		Goods: goods,
	}, nil
}
